<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of usuario
 *
 * @author jorge-luis
 */
class usuario extends CI_Model {

    private $us = array();
    private $psw = array();

    function __construct() {
        parent::__construct();
    }

    function guardar($datos) {
        $this->db->insert('usuario', $datos);
        return $this->db->insert_id();
    }

    function editar($datos) {
        $id = $this->input->post('id');
        $this->db->where('usuario.id', $id);
        $this->db->update('usuario', $datos);
    }

    function eliminar($id) {
        $this->db->where('usuario.id', $id);
        $this->db->delete('usuario');
    }

    function buscar() {
        $this->db->select('*');
        $this->db->from('usuario');
        $lista = $this->db->get();
        if ($lista->num_rows() > 0) {
            return $lista->result_array();
        } else {
            return null;
        }
    }

    function buscarByClave($clave) {
        $this->db->select('*');
        $this->db->from('usuario');
        $this->db->where('usuario.clave', $clave);
        $lista = $this->db->get();
        if ($lista->num_rows() > 0) {
            $fila = $lista->row_array();
            $this->psw['id'] = $fila['id'];
        }
    }

    function buscarByClaveUsername($username, $clave) {
        $this->buscarByClave($clave);
        $this->buscarByUsername($username);
        if($this->us!=null && $this->psw!=null){
        if ($this->us['id'] !== $this->psw['id']) {
            $_SESSION['id'] = -1;
            $_SESSION['username'] = -1;
        }
        }else{
            $_SESSION['id'] = -1;
            $_SESSION['username'] = -1;
        }
    }

    function buscarByUsername($username) {
        $this->db->select('*');
        $this->db->from('usuario');
        $this->db->where('username',$username);
        $lista = $this->db->get();
        if ($lista->num_rows() > 0) {
            $fila = $lista->row_array();
            $_SESSION['id'] = $fila['id'];
            $_SESSION['username'] = $fila['username'];
            $_SESSION['es_activo'] = $fila['es_activo'];
            $_SESSION['es_superUsuario'] = $fila['es_superUsuario'];
            $this->us['id'] = $fila['id'];
        }
    }

}
